import { loginAPI, getUserInfoAPI } from '@/api/user'

export default {
  namespaced: true,
  state: {
    token: null,
    userId: null,
    userInfo: {},
    time1: 0
  },
  mutations: {
    setToken(state, token) {
      state.token = token
    },
    removeToken(state) {
      state.token = null
    },
    getUserId(state, userId) {
      state.userId = userId
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    setTime(state, time) {
      state.time1 = time
    }
  },
  actions: {
    async loginAction(context, data) {
      const res = await loginAPI(data)
      // console.log(res)
      context.commit('setToken', res.data.token)
      context.commit('getUserId', res.data.userId)
      context.commit('setTime', Date.now()) // 登录存当前时间，持久话
    },
    async getUserInfoAction({ commit }, id) {
      const res = await getUserInfoAPI(id)
      // console.log(res)
      commit('setUserInfo', res.data)
    }
  }

}
